package com.qf.controller ;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.User;
import com.qf.entity.UserAddress;
import com.qf.entity.UserAddressDTO;
import com.qf.service.UserAddressService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * --- 包的 ---
 *
 * @author ding
 * @date 2025/5/4
 * @desc
 */
@RestController
@RequestMapping("/user")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;
    /*
    * 查询地址全部信息
    * */
    @GetMapping("/findAll/address")
    public R findAllAddress(UserAddressDTO userAddressDTO){
        System.out.println("userAddressDTO = " + userAddressDTO);
        // 1. 开启分页
        PageHelper.startPage(userAddressDTO.getPageNum(), userAddressDTO.getPageSize());
        // 2. 查询用户地址表中所有的数据，返回的是一个List集合
        List<UserAddress> list= userAddressService.findAllAddress(userAddressDTO);
        System.out.println("list = " + list);
        // 3. 将List集合装进pageInfo里
        PageInfo<UserAddress> pageInfo = new PageInfo<>(list);
        // 4. 将pageInfo返回(pageInfo就是分页后的结果)
        return R.ok(pageInfo);
    }
    /*
    * 删除地址信息
    * */
    @GetMapping("/delete/address")
    public R deleteAddress(int id){
        boolean ok = userAddressService.deleteAddress(id);
        return R.ok(ok);
    }
    /*
    * 修改地址信息
    * */
    @PostMapping("/update/address")
    public R updateAddress(@RequestBody UserAddress userAddress){
        boolean ok = userAddressService.updateAddress(userAddress);
        return R.ok(ok);
    }
    /*
    * 批量删除地址信息
    * */
    @GetMapping("/batch/delete/address")
    public R batchDeleteAddress(@RequestParam List<Integer> ids){
        //System.out.println("ids = " + ids);
        boolean ok = userAddressService.batchDeleteAddress(ids);
        return R.ok(ok);
    }
    /*
    * 添加地址信息
    * */
    @PostMapping("/add/address")
    public R addAddress(@RequestBody UserAddress userAddress){
        boolean ok = userAddressService.addAddress(userAddress);
        return R.ok(ok);
    }
}
